<?php
/**
 * SimplifyPHP Framework
 *
 * This file is part of SimplifyPHP Framework.
 *
 * SimplifyPHP Framework is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * SimplifyPHP Framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Rodrigo Rutkoski Rodrigues <rutkoski@gmail.com>
 */

require_once (dirname(__file__) . '/IDictionary.php');

/**
 * Dictionary.
 *
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 * @package Simplify_Kernel_Data
 */
class Dictionary implements IDictionary
{

  /**
   *
   * @var integer
   */
  const FILTER_NULL = 1;

  /**
   *
   * @var integer
   */
  const FILTER_EMPTY = 2;

  /**
   * Holds data.
   *
   * @var array
   */
  protected $data = array();

  /**
   * Constructor.
   *
   * @param array $data Initial data.
   * @return void
   */
  //public function __construct($data = null, $except = null, $filter = null)
  public function __construct($data = null)
  {
    $args = func_get_args();
    $except = sy_get_param($args, 1);
    $filter = sy_get_param($args, 2);
    
    $this->reset($data, $except, $filter);
  }

  /**
   * Automagicaly calls Dictionary::get().
   *
   * @param string $name The key.
   * @return mixed The value.
   */
  public function __get($name)
  {
    return $this->get($name);
  }

  /**
   * Automagically calls Dictionary::set().
   *
   * @param string $name The key.
   * @param mixed $value The value.
   * @return void
   */
  public function __set($name, $value)
  {
    $this->set($name, $value);
  }

  /**
   * Automagically calls Dictionary::has().
   *
   * @param string $name The key.
   * @return boolean
   */
  public function __isset($name)
  {
    return $this->has($name);
  }

  /**
   * Automagically calls Dictionary::del().
   *
   * @param string $name The key.
   * @return void
   */
  public function __unset($name)
  {
    $this->del($name);
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/IDictionary#copyAll($data, $ignore, $ignoreIfEmpty)
   */
  //public function copyAll($data, $except = null, $filter = null)
  public function copyAll($data)
  {
    $args = func_get_args();
    $except = sy_get_param($args, 1);
    $filter = sy_get_param($args, 2);
    
    if ($data instanceof IDictionary)
      $data = $data->getAll();
    
    if (! is_array($except))
      $except = array(
        $except 
      );
    
    if (! is_array($data))
      $data = array(
        $data 
      );
    
    foreach ($data as $name => $value) {
      if (! empty($except) && in_array($name, $except))
        continue;
      if ($filter == Dictionary::FILTER_NULL && is_null($value))
        continue;
      if ($filter == Dictionary::FILTER_EMPTY && empty($value))
        continue;
      
      $this->set($name, $value);
    }
    
    return $this;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/IDictionary#del($name)
   */
  public function del($name)
  {
    if (isset($this->data[$name])) {
      unset($this->data[$name]);
    }
    else {
      throw new NotFoundException('Not found');
    }
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/IDictionary#get($name, $default, $defaultIfEmpty)
   */
  //public function get($name, $default = null, $filter = null)
  public function get($name)
  {
    $args = func_get_args();
    $default = sy_get_param($args, 1);
    $filter = sy_get_param($args, 2);
    
    if (! $this->has($name, $filter))
      return $default;
    
    return $this->data[$name];
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/IDictionary#getAll()
   */
  public function getAll()
  {
    return $this->data;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/IDictionary#has($name, $checkEmpty)
   */
  //public function has($name, $filter = null)
  public function has($name)
  {
    $args = func_get_args();
    $filter = sy_get_param($args, 1);
    
    if (! isset($this->data[$name]))
      return false;
    
    if ($filter == self::FILTER_NULL && is_null($this->data[$name]))
      return false;
      
    if ($filter == self::FILTER_EMPTY && empty($this->data[$name]))
      return false;
      
    return true;
  }

  /**
   * Parse $data as a Data Holder.
   *
   * @param mixed $data $data for the Data Holder.
   * @return DataHolder
   */
  public static function parseFrom($data)
  {
    if (is_a($data, 'Dictionary'))
      return $data;
    if (method_exists($data, 'getAll'))
      $data = $data->getAll();
    return new Dictionary($data);
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/IDictionary#reset($data)
   */
  //public function reset($data = null, $except = null, $filter = null)
  public function reset($data = null)
  {
    $args = func_get_args();
    $except = sy_get_param($args, 1);
    $filter = sy_get_param($args, 2);
    
    $this->data = array();
    if (! empty($data))
      $this->copyAll($data, $except, $filter);
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/IDictionary#set($name, $value)
   */
  public function set($name, $value = null)
  {
    $this->data[$name] = $value;
  }

}

?>